//
//  PlaceViewController.m
//  Trip
//
//  Created by dlios on 15-7-14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PlaceViewController.h"
#import "PlaceTacticsTableViewCell.h"
#import "PlaceInfViewController.h"
#import "XingChengViewController.h"
#import "LvXingDiViewController.h"
#import "GNViewController.h"
#import "GWViewController.h"
#import "Tactics.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "UIViewController+Example.h"
#import "MJRefresh.h"

static const CGFloat MJDuration = 3.0;
@interface PlaceViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *myTableView;
@end

@implementation PlaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationItem.title = [[self.souArr objectAtIndex:0] objectForKey:@"name_zh_cn"];
    self.view.backgroundColor = [UIColor whiteColor];
    [self headlerData];
    [self setTabView];
    [self exampleUpPush];
}

- (void)headlerData
{
    NSString *str1 = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json", self.str];
    [AFNetworkHandler GetWithAFNByURL:str1 completion:^(id result) {
       
        self.souArr = result;
        self.navigationItem.title = [[self.souArr objectAtIndex:0] objectForKey:@"name_zh_cn"];
        [self.myTableView reloadData];
    }];
}

- (void)exampleUpPush
{
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        [self loadNewData];
        
    }];
}

- (void)loadNewData
{
    [self headlerData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.myTableView reloadData];
        
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.myTableView.header endRefreshing];
    });
}

//- (void)loadMoreData
//{
//    // 1.向上拖动时page自加
//    page++;
//    [self headlerData];
//    // 2.模拟2秒后刷新表格UI（真实开发中，可以移除这段gcd代码）
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        // 刷新表格
//        [self.myTableView reloadData];
//        // 拿到当前的上拉刷新控件，结束刷新状态
//        [self.myTableView.footer endRefreshing];
//    });
//}

- (void)setTabView
{
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.view addSubview:_myTableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 280;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.souArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    PlaceTacticsTableViewCell *placeCell = [[PlaceTacticsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
//    [placeCell.image1 setImageWithURL:[NSURL URLWithString:[[self.souArr objectAtIndex:indexPath.item] objectForKey:@"image_url"]]];
    [placeCell.image1 setImageWithURL:[NSURL URLWithString:[[self.souArr objectAtIndex:indexPath.item] objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
    placeCell.image1.layer.cornerRadius = 10;
    placeCell.image1.clipsToBounds = YES;
    placeCell.label1.text = [[self.souArr objectAtIndex:indexPath.item] objectForKey:@"name_zh_cn"];
    placeCell.label1.textColor = [UIColor whiteColor];
    placeCell.label2.text = [[self.souArr objectAtIndex:indexPath.item] objectForKey:@"name_en"];
    placeCell.label2.textColor = [UIColor whiteColor];
    [placeCell.label3 setTitle:@"行程" forState:UIControlStateNormal];
    [placeCell.label3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [placeCell.label3 addTarget:self action:@selector(lab3Action:) forControlEvents:UIControlEventTouchUpInside];
//    [placeCell.label4 setTitle:@"行程" forState:UIControlStateNormal];
//    [placeCell.label4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [placeCell.label4 addTarget:self action:@selector(lab4Action:) forControlEvents:UIControlEventTouchUpInside];
    [placeCell.label5 setTitle:@"旅行地" forState:UIControlStateNormal];
    [placeCell.label5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [placeCell.label5 addTarget:self action:@selector(lab5Action:) forControlEvents:UIControlEventTouchUpInside];
    [placeCell.image2 addTarget:self action:@selector(lab3Action:) forControlEvents:UIControlEventTouchUpInside];
    [placeCell.image2 setBackgroundImage:[UIImage imageNamed:@"iconfont-importedlayers.png"] forState:UIControlStateNormal];
//    [placeCell.image3 addTarget:self action:@selector(lab4Action:) forControlEvents:UIControlEventTouchUpInside];
//    [placeCell.image3 setBackgroundImage:[UIImage imageNamed:@"iconfont-importedlayers.png"] forState:UIControlStateNormal];
    [placeCell.image4 addTarget:self action:@selector(lab5Action:) forControlEvents:UIControlEventTouchUpInside];
    [placeCell.image4 setBackgroundImage:[UIImage imageNamed:@"iconfont-mudedi.png"] forState:UIControlStateNormal];
    return placeCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 点击cell的方法, 选中之后灰色条消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    PlaceInfViewController *placeInfVC = [[PlaceInfViewController alloc] init];
//    placeInfVC.str = [[self.souArr objectAtIndex:indexPath.row] objectForKey:@"id"];
//    NSLog(@"qqq%@", placeInfVC.str);
//    XingChengViewController *xcVC = [[XingChengViewController alloc] init];
//    xcVC.str = [[self.souArr objectAtIndex:indexPath.row] objectForKey:@"id"];
//    LvXingDiViewController *lxdVC = [[LvXingDiViewController alloc] init];
//    lxdVC.str = [[self.souArr objectAtIndex:indexPath.row] objectForKey:@"id"];
//}

- (void)lab3Action:(UIButton *)button
{
    XingChengViewController *xcVC = [[XingChengViewController alloc] init];
    UITableViewCell *cell = (UITableViewCell *)[[button superview]superview];
    NSIndexPath *indePath = [self.myTableView indexPathForCell:cell];
    xcVC.str = [[self.souArr objectAtIndex:indePath.row] objectForKey:@"id"];
    xcVC.title = [[self.souArr objectAtIndex:indePath.row] objectForKey:@"name_zh_cn"];
    [self.navigationController pushViewController:xcVC animated:YES];
}

- (void)lab4Action:(UIButton *)button
{
    XingChengViewController *xcVC = [[XingChengViewController alloc] init];
    UITableViewCell *cell = (UITableViewCell *)[[button superview]superview];
    NSIndexPath *indePath = [self.myTableView indexPathForCell:cell];
    xcVC.str = [[self.souArr objectAtIndex:indePath.row] objectForKey:@"id"];
    [self.navigationController pushViewController:xcVC animated:YES];
}

- (void)lab5Action:(UIButton *)button
{
    LvXingDiViewController *lxdVC = [[LvXingDiViewController alloc] init];
    UITableViewCell *cell = (UITableViewCell *)[[button superview]superview];
    NSIndexPath *indePath = [self.myTableView indexPathForCell:cell];
    lxdVC.str = [[self.souArr objectAtIndex:indePath.row] objectForKey:@"id"];
    lxdVC.title = [[self.souArr objectAtIndex:indePath.row] objectForKey:@"name_zh_cn"];
    [self.navigationController pushViewController:lxdVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
